<?php
/**
 * Created by PhpStorm.
 * User: tyong
 * Date: 2018/10/14
 * Time: 21:38
 */
/**
 * @return mixed
 * 处理菜单列表 加入高亮状态
 */
function get_menus() {
    define('CONTROLLER_NAME','Member');
    define('ACTION_NAME','index');
    $menus = config('menus.MENUS');
    //数据填充空字符串
    foreach($menus as $key_a => &$menu) {
        $menu['group']['url']=empty($menu['group']['url'])?'':$menu['group']['url'];
        $menu['group']['_child']=empty($menu['group']['_child'])?[]:$menu['group']['_child'];
        $menu['group']['class']='';
        if (!empty($menu['_child'])) {
            foreach ($menu['_child'] as &$child) {
                $child['class']='';
                $child['check_level']=empty($child['check_level'])?'':$child['check_level'];
            }
        }
    }
    //处理每个菜单的信息
    foreach($menus as $key_a => &$menu) {
        //该判断主要是 针对没有子菜单的父菜单
        if (false !== strpos($menu['group']['url'], CONTROLLER_NAME)) {
            $menu['group']['class'] = 'active'; //高亮设置
            break; //结束当前循环
        }
        //存在子菜单
        if (!empty($menu['_child'])) {
            //循环子菜单 判断高亮
            foreach ($menu['_child'] as $key_b => &$child) {
                //如果当前控制器和方法在  映射设置里边 则设置其映射菜单为高亮
                if(in_array(CONTROLLER_NAME . '/' . ACTION_NAME, config('menus.CURRENT')) || in_array(CONTROLLER_NAME, config('menus.CURRENT'))) {
                    //获取 当前控制器或者控制器+方法 在CURRENT设置中的键值 以获取映射中的值
                    $c_k1 = array_keys(C('CURRENT'), CONTROLLER_NAME . '/' . ACTION_NAME);
                    $c_k2 = array_keys(C('CURRENT'), CONTROLLER_NAME);
                    //控制器+方法 优先
                    $m_k  = !empty($c_k1) ? $c_k1[0] : $c_k2[0];
                    //设置其映射菜单为高亮
                    $mapping = config('menus.MAPPING');
                    if(false !== strpos($child['url'], $mapping[$m_k])) {
                        $menu['group']['class'] = $child['class'] = 'active';
                        break 2; //结束两层循环
                    }
                }
                //拆分路径为 控制器和方法的数组
                $urls = explode('/', $child['url']);
                //如果check_level级别为1 则判断 控制器+方法  菜单高亮
                if($child['check_level'] == 1) {
                    //控制器和方法都能对应上
                    if(in_array(CONTROLLER_NAME, $urls) && in_array(ACTION_NAME, $urls)){
                        $menu['group']['class'] = $child['class'] = 'active';
                        break 2;
                    }
                    continue;
                }
                //判断当前控制器在那个菜单路径下 该菜单高亮
                if (in_array(CONTROLLER_NAME, $urls)) {
                    $menu['group']['class'] = $child['class'] = 'active';
                    break 2;
                }
            }
        }
    }
    return $menus;
}
